//
//  UploadLiveBgSongsManger.m
//  BuxProject
//
//  Created by iOS的 on 2021/12/28.
//  Copyright © 2020 Moitnea. All rights reserved.
//

#import "UploadLiveBgSongsManger.h"
#import <AVFoundation/AVAsset.h>
#import <AVFoundation/AVMetadataItem.h>
#import "MainPublickTools.h"
@implementation UploadLiveBgSongsManger

+ (instancetype)instance {
    static UploadLiveBgSongsManger *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc] init];
    });
    return _manager;
}

- (NSMutableArray <NSDictionary *>*)choseLocalSongArr {
    NSMutableArray *lists = [NSMutableArray array];
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"ChoseLiveSoneArray"];
    NSMutableArray* selectedArray = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePaths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSError *error = nil;
    NSArray *fileList = [fileManager contentsOfDirectoryAtPath:filePaths error:&error];;
    for (NSString *fileName in fileList) {
        if ([fileName containsString:@".mp3"]) {
            NSMutableDictionary *model = [[NSMutableDictionary alloc] init];
            NSString *musicFilePath = [filePaths stringByAppendingPathComponent:fileName];
            NSURL *musicURL = [NSURL fileURLWithPath:musicFilePath];
            AVURLAsset *musicAsset = [AVURLAsset URLAssetWithURL:musicURL options:nil];
            CMTime duration = musicAsset.duration;
            float musicDurationSeconds = CMTimeGetSeconds(duration);
            int minute = (int)musicDurationSeconds/60;
            int second = (int)musicDurationSeconds%60;
            
            [model setObject:[NSString stringWithFormat:@"%d分%d秒",minute,second] forKey:@"duration"];
            [model setObject:musicFilePath forKey:@"url"];
            for (NSString *format in [musicAsset availableMetadataFormats]) {
                for (AVMetadataItem *metadataItem in [musicAsset metadataForFormat:format]) {
                    if ([metadataItem.commonKey isEqualToString:@"title"]) {
                        NSString * title = (NSString *)metadataItem.value;
                        if ([fileName containsString:title]) {
                            [model setObject:title forKey:@"name"];
                        }
                         
                    } else if ([metadataItem.commonKey isEqualToString:@"artist"]) {
                        NSString *artist = (NSString *)metadataItem.value;
                        [model setObject:artist forKey:@"artist"];
                    } else if ([metadataItem.commonKey isEqualToString:@"albumName"]) {
                        NSString *albumName = (NSString *)metadataItem.value;
                        [model setObject:albumName forKey:@"musicAlbum"];
                    }
                }
            }
            
            if([MainPublickTools ObjectIsNull:[model objectForKey:@"artist"]]){
                [model setObject:@"未知艺术家" forKey:@"artist"];
            }
            NSDictionary * dictItems = [fileManager attributesOfItemAtPath:musicFilePath error:nil];
            NSString * fileSize = [NSString stringWithFormat:@"%@",dictItems[@"NSFileSize"]];
            [model setObject:fileSize forKey:@"size"];
            NSString *nameStr = [model objectForKey:@"name"];
            [model setObject:(![MainPublickTools ObjectIsNull:nameStr] ? nameStr: [fileName substringToIndex:fileName.length - 4]) forKey:@"name"];
            
            if ([selectedArray containsObject:nameStr]) {
                [model setObject:@(YES) forKey:@"isSelected"] ;
            } else {
                [model setObject:@(NO) forKey:@"isSelected"] ;
            }
            
            [lists addObject:model];
        }
    }
    return lists;
}

- (NSMutableArray <NSDictionary *>*)obtainChoseSongArr {
    NSMutableArray *selectedArray = [NSMutableArray array];
    NSMutableArray *allList = [self choseLocalSongArr];
    for (NSDictionary *model in allList) {
        
        if (model[@"isSelected"] &&[model[@"url"] containsString:@"/"]) {
            [selectedArray addObject:model];
        }
    }
    return selectedArray;
}

@end
